
<template>
  <div class="user-container">
   <el-card class="box-card">
      <el-row style="margin: 10px">
        <el-col :span="24">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="addClick"
            >添加</el-button
          >
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="deleteClick"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="userList" style="width: 100%" stripe  lazy border @selection-change="handleSelectionChange">>
        <el-table-column
      type="selection"
      width="55">
    </el-table-column>
        <el-table-column label="头像" width="80" align="center">
          <template v-slot="slot">
            <el-image
              style="width: 50px; height: 50px"
              :preview-src-list="[slot.row.avatar]"
              :src="slot.row.avatar"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="user_name" label="用户名" width="130" align="center"> </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180" align="center"></el-table-column>
        <el-table-column prop="mobile" label="手机" width="110" align="center"></el-table-column>
        <el-table-column label="用户状态" width="80" align="center">
          <template v-slot="slot">
            <!-- <el-tag type="success" v-if="slot.row.status">正常</el-tag>
            <el-tag v-else>限制</el-tag> -->

            <el-switch
              v-model="slot.row.status" @change="disable(slot.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column  label="角色" width="80" align="center">
          <template v-slot="slot">
            <el-tag type="success" v-for="(item,index) in slot.row.roles " :key="index" style="margin:3px">{{item.name}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="createdAt"
          label="注册时间"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="address" label="操作"  align="center">
          <template v-slot="slot">
            <el-button type="primary" icon="el-icon-edit" size="small"
            @click="editClick(slot.row.id)" >编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="small"
             @click="deleteClick(slot.row)"  >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
       <!-- 添加抽屉 -->
        <el-drawer
        title="添加用户"
        :visible.sync="adddrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="70px"
          :model="addUserForm"
          size="small"
        >
        
          <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="用户名">
            <el-input v-model="addUserForm.user_name"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
             <el-form-item label="密码">
            <el-input v-model="addUserForm.password" type="password"></el-input>
          </el-form-item>
            </el-col>
          </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="邮箱">
            <el-input v-model="addUserForm.email"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="手机">
            <el-input v-model="addUserForm.mobile"></el-input>
          </el-form-item>
            </el-col>
          </el-row>
           <el-form-item label="状态">
            <el-switch v-model="addUserForm.status"> </el-switch>
          </el-form-item>
        
          <el-form-item>
           <el-button size="small" type="primary" @click="addUser">添加</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
       <!-- 编辑抽屉 -->
        <el-drawer
        title="编辑用户"
        :visible.sync="editdrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="70px"
          :model="editUserForm"
          size="small"
        >
        
          <el-row :gutter="20">
            <el-col :span="12">
                <el-form-item label="用户名">
            <el-input v-model="editUserForm.user_name"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
             <el-form-item label="密码">
            <el-input v-model="editUserForm.password" type="password"></el-input>
          </el-form-item>
            </el-col>
          </el-row>
        <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="邮箱">
            <el-input v-model="editUserForm.email"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="手机">
            <el-input v-model="editUserForm.mobile"></el-input>
          </el-form-item>
            </el-col>
          </el-row>

           <el-form-item label="状态">
            <el-switch v-model="editUserForm.status"> </el-switch>
          </el-form-item>
        
          <el-form-item>
           <el-button size="small" type="primary" @click="editUser">修改</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { getUserList ,deleteUser,addUser,getUserId,editUser} from '@/api/user';
export default {
  name: 'user',
data() {
    return {
      userList: [],// 文章表格数据
       queryInfo: {
        query: '', // 查询参数
        pageNum: 1,// 分页页码
        pageSize: 10// 页面数据条数
      },
      total: 0,
      adddrawer:false,
      editdrawer: false, //编辑抽屉显示of隐藏
       direction: "rtl",
      labelPosition: "left",//改变表单域标签的位置
      editUserForm: {}, // 编辑的数据
       addUserForm:{
         user_name: '',
          email: null,
          mobile: null,
          password:'123456',
          status:true
       }, // 添加的数据
      selectRows: []// 全选数据
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 滑块功能
    async disable(row) {
      const { id, status } = row;
      const res = await editUser(id, { status: status });
      this.$message.success(`${status ? "该用户状态设置为正常" : "已拉黑该用户，并已限制该用户登录"}`);
    },
    // 确认修改按钮
    async editUser(){
      const id =this.editUserForm.id
      await editUser(id,this.editUserForm)
      this.editdrawer = !this.editdrawer;
      this.getUserList()
      this.$message.success('修改成功')
    },
     // 编辑弹出抽屉
   async editClick(id) {
       // 2发请求 把得到的数据赋值给data
      const res = await getUserId(id)
      this.editUserForm = res
       // 1，弹出抽屉
      this.editdrawer = !this.editdrawer;
    },
    // 确认添加按钮
    async addUser(){
      // const body = this.addLinkform
     const res =  await addUser(this.addUserForm)
      this.$message.success('添加成功')
      this.adddrawer = !this.adddrawer;
      this.getUserList();
    },
    // 点击添加按钮弹出抽屉
    addClick(){
      this.adddrawer = !this.adddrawer;
    },
    // 关闭抽屉就清空
    close(){
      this.editUserForm = {}
      this.addUserForm={
         user_name: '',
          email: null,
          mobile: null,
          password:'123456',
          status:true
       }
    },
    // 用户表格数据
    async getUserList() {
      const { list, pageNum, pageSize, total } = await getUserList(this.queryInfo);
      this.userList = list;
      this.queryInfo.pageNum =Number(pageNum);
      this.queryInfo.pageSize =Number(pageSize);
      this.total = total;
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.getUserList();
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum=val
      this.getUserList();
    },
    // 删除用户
    deleteClick(row){
      if (row.id) {
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteUser({ ids: [row.id] });
            this.getUserList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      } else {
        // 批量删除
        if (!this.selectRows.length)
          return this.$message.warning("请选择要删除的选中项");
        const ids = this.selectRows.map((item) => item.id);
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteUser({ ids });
            this.getUserList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      }
    },
    // 关闭抽屉提示
    handleClose(done) {
      done();
      // this.$confirm("确认关闭？")
      //   .then((_) => {
      //     done();
      //   })
      //   .catch((_) => {});
    },
    // 全选数据
    handleSelectionChange(val) {
      this.selectRows=val
      }
  },
};
</script>

<style lang="scss">
.box-card {
  margin: 20px;
}
.el-table{
  margin-bottom: 20px;
}
.el-image{
  border-radius: 50%;
}
.el-form{
  margin: 40px 8px;
}
</style>